package com.bbcare.followup.plat.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.followup.plat.entity.FollowQueue;
import com.bbcare.followup.plat.entity.PostObjectPolicy;
import com.bbcare.followup.plat.service.IFollowQueueService;
import com.bbcare.followup.plat.service.impl.AliyunOSSClient;

@RestController
@RequestMapping("/webapi/service/aliyun/oss")
public class AliyunOSSRestWebapi {
		private final Log logger = LogFactory.getLog(AliyunOSSRestWebapi.class);
	    @Autowired
	    private AliyunOSSClient aliyunOSSClient ;
	    @Autowired
	    private IFollowQueueService fllowQueueService;
	    
	    /**
	     * @author wuyz
	     * @throws Exception 
	     * @time 2018-04-28
	     * 获取当前阿里云，给前端上传oss文件 返回鉴权策略
	     * 
	     **/
	    @ResponseBody
	    @RequestMapping("/getPolicyWebapi")
	    public ResultModel<PostObjectPolicy> getPolicyWebapi(@RequestBody String arg, HttpServletRequest request) throws Exception {
	    	logger.info("getPolicyWebapi  : "+arg);
	    	 JSONObject jsonArg = null;
	         try {
	             jsonArg = JSON.parseObject(arg);
	         } catch (Exception ex) {
	             ex.printStackTrace();
	             throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
	         }

	         JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
	         if (null == jsonData) {
	             // 前台必须传data节点，否则报错返回
	             throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
	         }
	         
	        String queueId = jsonData.getString("queueId"); 
	        if (StringUtils.isBlank(queueId)) {
	             throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "队列id不存在"));
	         }
	        String type = jsonData.getString("type"); 
	         // 创建人，从session中获取
	        Map<String,Object> parma = new HashMap<String,Object>();
	        parma.put("id",queueId);
	        FollowQueue depObj = fllowQueueService.getQueueDetailByQueueId(parma);
			if(null == depObj){
				throw new AppException(ErrorCode.DEPARTMENT_EXCEPTION_TYPE_DEPARTMENT_NOT_EXIST,"队列信息不存在");
			}
			String tenantId =depObj.getTenantId();
	        Map<String,Object> map = new HashMap<String,Object>();
	        map.put("tenantId", tenantId);
	        map.put("time", Constants.ACCOUNT_LOGIN_SESSION_TIMEOUT);
	        map.put("type", type);
	    	PostObjectPolicy postobjectpolicy = aliyunOSSClient.getPostObjectPolicy(map);
	    	postobjectpolicy.setTenantId(tenantId);
	    	return new ResultModel<PostObjectPolicy>(postobjectpolicy);
	    }
}
